package com.lock8;

import java.util.concurrent.TimeUnit;

/*
* 8锁，关于8个锁的问题
* 1，标准情况下，两个线程先打印，发短信还是打电话，1.发短信，2，打电话
* 2，sendSms 延迟4秒，两个线程，先打印谁
* */
public class Test01 {
    public static void main(String[] args) {
        Phone phone = new Phone();

        new Thread(()->{
            phone.sendSms();
        },"A").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            phone.call();
        },"B").start();

        new Thread(()->{
            phone.hello();
        },"C").start();
    }
}

class Phone{
    //synchronized 锁的对象是方法的调用者！
    //两个方法用的是同一个锁，谁先拿到谁执行！
    public synchronized void sendSms(){
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("sendSms");
    }

    public synchronized void call(){
        System.out.println("call");
    }

    public void hello(){
        System.out.println("hello");
    }
}

